import { saveToLocal, loadFromLocal } from './local-storage'
import store from './../store'

// import Mock from 'mockjs'

const TokenKey = 'Vue-cms'
const UserKey = 'user'
const PermissionKey = 'permissions'

function getToken() {
  if (store) {
    console.log(store.getters.token)
    if (store.getters && store.getters.token) {
      return store.getters.token
    }
  }
  return loadFromLocal(TokenKey)
}

function setToken(token) {
  return saveToLocal(TokenKey, token)
}

function removeToken() {
  return saveToLocal(TokenKey, '')
}

function setPermissions(permissions) {
  saveToLocal(PermissionKey, permissions)
}

function getPermissions() {
  return loadFromLocal(PermissionKey)
}

function setUser(user) {
  return saveToLocal(UserKey, user)
}

function getUser() {
  return loadFromLocal(UserKey)
}

function removeUser() {
  return saveToLocal(UserKey, '')
}

export {
  getToken,
  setToken,
  removeToken,
  setUser,
  getUser,
  removeUser,
  setPermissions,
  getPermissions
}
